<template>
  <div>
    <div class="mycomment">
      <!-- 这里添加的是下拉涮洗的组件 -->
      <mt-loadmore :top-method="loadTop" ref="loadmore" @top-status-change="handleTopChange">
        <div class="mycomment-content" v-for="(item,index) in dataList" :key="index">

          <div class="mycomment-content-right" @click="goArtical(item.post_id)">
            <p>{{item.create_time}}</p>
            <p>{{item.content}}</p>
            <div class="mycomment-content-right-content">
              <div class="mycomment-content-right-content-top">
                <p>{{item.title}}</p>
              </div>
              <div class="mycomment-content-right-content-bottom">
                <div class="mycomment-content-right-icon clearfix">
                  <a href="javascript:void(0)"><img src="../../../assets/comment/pinglun@2x.png" alt="">
                    <span class="red">{{item.num }}</span>
                  </a>
                  <a href="javascript:void(0)"><img src="../../../assets/comment/guanzhu@2x.png" alt="">
                    <span>{{item.favor_counts }}</span>
                  </a>
                  <a href="javascript:void(0)"><img src="../../../assets/comment/dianzan@2x.png" alt="">
                    <span>{{item.like_counts }}</span>
                  </a>
                  <a href="javascript:void(0)">
                    <img src="../../../assets/comment/liulanliang@2x.png" alt="">
                    <span>{{item.reads_count }}</span>
                  </a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </mt-loadmore>
      <loading title="加载中..."  v-if="loadMoreShow"></loading>
      <Nomore v-if="noMoreShow"></Nomore>
      <div class="showimg" v-if="this. dataList.length==0">
        <img src="../../../assets/nothing.png" alt="">
        <p>空空如也~</p>
        <p>
          <a href="javascript:void(0)" @click="skit">去逛逛论坛?</a>
        </p>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import { MessageBox, Toast,Indicator } from 'mint-ui';
import '../../../js/lib/flexble.js'
import commons from '../../../common/js/common.js'
export default {
  data() {
    return {
      loadMoreShow: false,
      topStatus: '',
      pageNum: 1,
      edit: false,
      selectNum: 0,
      dataList: [],
      selectNum: 0,
      noMoreShow: false,
      arr: [],
      idStr: '',
      flag: true
    }
  },
  created() {
    var _this = this;
    this.$nextTick(function() {
      window.addEventListener('scroll', onScroll)
    })
    // 判断滚动条位置，加载更多
    function onScroll() {
      var scrollTop = document.body.scrollTop;
      var innerHeight = window.innerHeight;
      var offsetHeight = document.body.offsetHeight;
      if (scrollTop + innerHeight >= offsetHeight) {
        if (_this.flag) {
          if (_this.dataList.length >= 7) {
            _this.flag = false;
            _this.getList()
          }
        }
      }
    }
  },
  mounted() {
    Vue.http.interceptors.push((request, next) => { //请求拦截器
      //请求前
      request.headers.set('token', commons.getCookie('token')); //设置请求头
      Indicator.open('加载中...'); //请求loading
      next((response) => {
        //请求后
        Indicator.close(); //隐藏loading
        return response
      })
    })
    this.mycomment();
  },
  methods: {
    handleTopChange: function(status) {  //注意这个是下拉组件中的方法
      this.topStatus = status;
    },

    loadTop() {  // 下拉刷新数据的操作
      setTimeout(() => {
        this.mycomment();
        this.$refs.loadmore.onTopLoaded();
      }, 500);
    },
    goArtical(id) {  //点击评论跳转到想对应得页面
      var token = commons.getCookie('token');
      let postData = { visitType: 2, informationId: id, token: token, channel: window.sessionStorage.getItem('channel') ? window.sessionStorage.getItem('channel') : '' };
      this.$http.post('/loansupermarket-app/information/insertInformationVisit', postData).then(function(response) {
        if (response.body.code == '000') {//帖子详情
          // this.$router.push({ path: '/main/mydiscover/artical', query: { id: id } })
          window.location.href='../bbs/article.html?id='+id;
        } else if (response.body.code == '444') {
          // this.$router.push({ path: '/main/mydiscover/artical', query: { id: id } })
          window.location.href='../bbs/article.html?id='+id;
        }
      })
    },
    // 上拉加载数据
    getList() {
      this.pageNum += 1;
      this.loadMoreShow = true;
      var token = commons.getCookie('token');
      var _this = this;
      setTimeout(function() {
        _this.$http.post('/loansupermarket-app/personal/postListComment', { pageSize: 7, pageNum: _this.pageNum, token: token }, { headers: { token: token } })
          .then((response) => {
            var resBody = response.body;
            if (resBody.code == '000') {
              _this.flag = true;
              _this.loadMoreShow = false;
              if (resBody.data.length < 7) {
                _this.noMoreShow = true;
                _this.flag = false;
              } else {
                _this.flag = true;
              }
              _this.dataList = _this.dataList.concat(resBody.data);
              _this.dataList.forEach(function(item, index) {
                item.create_time = commons.formatDate1(item.create_time);
              })

            } else if (resBody.code == '444') {
              // this.$router.push({ path: '/login', query: { history: 1 } });
              window.location.href='../user/login.html?history=1';
            }
          })
      }, 500)
    },

    mycomment() {
      this.noMoreShow = false;
      var token = commons.getCookie('token');
      // 加载第一页数据并实例化
      let _this = this;
      this.$http.post('/loansupermarket-app/personal/postListComment', { pageSize: 7, pageNum: 1, token: token }, { headers: { token: token } })
        .then((response) => {
          var resBody = response.body;
          if (resBody.code == '000') {
            this.dataList.splice(0, resBody.data.length); //下拉请求新的数据，并且将第一页的数据删掉
            this.dataList = resBody.data.concat(this.dataList); //表示的是将最新的数据插在最前面
            if (this.dataList.length < 7) {
              this.flag = false;
              this.noMoreShow = true;
            } else {
              this.flag = true;
            }
            if (this.dataList.length == 0) {
              this.noMoreShow = false;
            }
            // 时间戳转成2018-10-08格式
          } else if (resBody.code == '444') {
            // this.$router.push({ path: '/login', query: { history: 1 } });
            window.location.href='../user/login.html?history=1';
          } else {
            MessageBox.alert(resBody.mesg);
          }

        })
    },
    skit() {  //当点击去逛逛论坛时跳转到相对于的页面
       window.location.href='../bbs/page.html'
    },
    // 去编辑
    goEdit() {
      this.edit = !this.edit;
      if (!this.edit) {
        this.selectNum = 0;
      }
    },
    //选择复选款的功能
    singleSelect() {
      var _this = this;
      var flag = true;
      var checkboxs = document.querySelectorAll('.ss');
      this.$refs.checkbox.forEach(function(item, index) {
        //checkboxs.forEach(function(item, index) {
        if (!item.checked) {
          _this.$refs.allSelect.checked = false;
          flag = false;
          return false;
        }
        if (flag) {
          _this.$refs.allSelect.checked = true;
        }
      })
      // 判断已选列表个数
      this.judgeNum();
    },
    // 判断已选列表个数
    judgeNum() {
      var _this = this;
      _this.selectNum = 0;
      var checkboxs = document.querySelectorAll('.ss');
      this.$refs.checkbox.forEach(function(item, index) {
        if (item.checked) {
          _this.selectNum += 1
        }
      })
    },
    // 如果是全选，那么所有的checkbox全部变成已选
    allSelect() {
      var _this = this;
      var checkboxs = document.querySelectorAll('.ss');
      if (this.$refs.allSelect.checked) {
        this.$refs.checkbox.forEach(function(item, index) {
          item.checked = true;
        })
      } else {
        this.$refs.checkbox.forEach(function(item, index) {
          item.checked = false;
        })
      }
      // 判断已选列表个数
      this.judgeNum();
    },
    shanchu() {
      var _this = this;
      var token = cookie.get("token");
      // 添加了一个属性cId，根据属性获取产品id再添加到数组提交到后台
      this.$refs.checkbox.forEach(function(item, index) {
        if (item.checked) {
          _this.arr.push(item.getAttribute('cId'));
          _this.idStr = _this.arr.join(',');
        }
      })
      if (this.idStr.length == 0) {
        Toast('请勾选你要删除的帖子~');
      } else {
        this.$http.post('/loansupermarket-app/information/deleteInformationVisit', { visitType: 1, idStr: this.idStr, token: token }).then(function(response) {
          if (response.body.code == '000') {
            Toast('删除成功！');
            setTimeout(function() {
              window.location.reload()
            }, 500)
          } else if (response.body.code == '444') {
            // this.$router.push({ path: '/login', query: { history: 1 } });
          } else {
            MessageBox.alert(response.body.mesg);
          }
        })
      }
    },
  },
}
</script>
<style lang="scss">
@import '../../../scss/base.scss';
.lo .weui-loadmore {
  margin: 0px auto;
  line-height: 0;
}

.lo {
  padding: 1.2em 0 0 0;
}

.mycomment {
  text-align: left;
  width: 100%;
  box-sizing: border-box;
  &-edit {
    position: fixed;
    right: 0.4rem;
    top: 0.35rem;
    z-index: 999;
    color: #fff;
    font-size: 0.4rem;
  }
  &-content {
    margin-bottom: rem(20);
    display: flex;
    padding: rem(30) 0.4rem;
    background: #ffffff;
    &-right {
      p:nth-of-type(1) {
        color: #999999;
        font-size: rem(24);
      }
      p:nth-of-type(2) {
        color: #333333;
        font-size: rem(28);
        margin: rem(18) 0 rem(30) 0; // 表示的意思的如果文字超过了一行的话，就省略...
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      &-content {
        box-sizing: border-box;
        background: #f5f5f5;
        border-radius: 8px;
        width: rem(650);
        &-top {
          padding-right: rem(20);
          width: 100%;
          height: rem(80);
          border-bottom: 1px solid #e3e3e3;
          p {
            margin-left: rem(30);
            font-size: rem(26);
            color: #666666;
            line-height: rem(80);
          }
        }
        &-bottom {
          width: 100%;
          height: rem(70); //margin-top: rem(24);
          line-height: rem(70);
        }
      }
      &-icon {
        a {
          float: right;
          display: block;
          margin-right: 10px;
          img {
            width: rem(35);
            height: rem(35);
            vertical-align: middle;
            padding-right: 2px;
            padding-bottom: 2px;
          }
          span {
            font-size: rem(24);
            color: #999999;
          }
        }
      }
    }
  }
}

.showimg {
  margin-top: rem(120);
  text-align: center;
  p {
    margin-top: rem(40);
    a {
      text-decoration: underline;
      font-size: rem(20);
      color: #00dada;
    }
  }
} // 这个是复选框的样式
.chk_1 {
  display: none; // vertical-align: -15%;
  width: 0.3rem;
  height: 0.3rem;
}

.chk_1+label {
  color: #fff !important;
  margin-top: 1.1rem;
  background-color: #FFF;
  border: 1px solid #C1CACA;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05),
  inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
  padding: 0.2rem;
  border-radius: 50%;
  display: inline-block;
  position: relative;
  width: 0.1rem;
  height: 0.1rem;
  margin-right: 10px;
}

.chk_1+label:active {

  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px 1px 3px rgba(0, 0, 0, 0.1);
}

.chk_1:checked+label {
  font-size: 0.17rem;
  margin-top: 1.1rem;
  background-color: $primaryColor;
  border: 1px solid $primaryColor;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
  color: #243441;
}

.chk_1:checked+label:after {
  content: '\2714'; //勾选符号 
  position: absolute;
  top: 0px;
  left: 0px;
  color: #fff;
  width: 100%;
  text-align: center;
  font-size: 1.4em;
  vertical-align: text-top;
} //这个是顶部删除的样式
.deleteDo {
  display: flex;
  width: 100%;
  position: fixed;
  left: 0;
  height: 1.3rem; // line-height: 1.3rem;
  color: #fff;
  bottom: 0;
  &-left {
    flex: 6;
    background: rgba(0, 0, 0, 0.4);
    border-right: 1px solid #fff;
    text-align: left;
    box-sizing: border-box;
    padding: 0 0.4rem;
    .chk_1+label {
      margin-top: 0.35rem;
      background-color: #FFF;
      border: 1px solid #C1CACA;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
      padding: 0.2rem;
      border-radius: 50%;
      display: inline-block;
      position: relative;
      margin-right: 30px; // display: none;
      // vertical-align: -15%;
      width: 0.1rem;
      height: 0.1rem;
    }
    .chk_1:checked+label {
      margin-top: 0.35rem;
      background-color: $primaryColor;
      border: 1px solid $primaryColor;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
      color: #243441;
    }
    span {
      float: right;
      line-height: 1.3rem;
      font-size: 0.3rem;
    }
  }
  &-right {
    text-align: center;
    font-size: 0.3rem;
    flex: 3;
    line-height: 1.3rem;
    background: rgba(0, 0, 0, 0.4)
  }
}
</style>


